package com.shujia;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.BitOP;

public class Demo03BitMap {

    Jedis jedis;

    @Before
    // 在@Test注解方法之前会运行一次
    public void init() {
        jedis = new Jedis("master", 6379);
    }

    @Test
    public void setBit() {
        // b1 0001 0001 1000 0000
        jedis.setbit("b1", 7, true);
        jedis.setbit("b1", 8, true);
        jedis.setbit("b1", 3, true);

        // b2 0101 0100 1000 0000
        jedis.setbit("b2", 1, true);
        jedis.setbit("b2", 3, true);
        jedis.setbit("b2", 5, true);
        jedis.setbit("b2", 8, true);
    }

    @Test
    public void getBit() {
        System.out.println(jedis.getbit("b1", 6));
        System.out.println(jedis.getbit("b1", 7));

        System.out.println(jedis.getbit("b2", 4));
        System.out.println(jedis.getbit("b2", 5));

    }

    @Test
    public void get() {
        System.out.println(jedis.get("rb1"));
        System.out.println(jedis.get("rb2"));
        System.out.println(jedis.get("rb3"));
        System.out.println(jedis.get("rb4"));
    }

    @Test
    public void bitOp() {
        // b1 0001 0001 1000 0000
        // b2 0101 0100 1000 0000

        // rb1 0001 0000 1000 0000
        jedis.bitop(BitOP.AND, "rb1", "b1", "b2");
        // rb2 0101 0101 1000 0000
        jedis.bitop(BitOP.OR, "rb2", "b1", "b2");
        // rb3 1110 1111 0111 1111
        jedis.bitop(BitOP.NOT, "rb3", "b1");
        // rb4 0101 0101 0000 0000
        jedis.bitop(BitOP.XOR, "rb4", "b1", "b2");
    }

    @Test
    public void getOpRes() {
        String[] rbArr = {"rb1", "rb2", "rb3", "rb4"};

        for (String rb : rbArr) {
            for (int i = 0; i <= 15; i++) {
                System.out.print(jedis.getbit(rb, i) ? 1 : 0);
            }
            System.out.println();
        }


    }


    @After
    // 在@Test注解方法之后会运行一次
    public void close() {
        jedis.close();
    }

}
